package Aleehot100;

/**
 * ClassName: c064
 * Package: Aleehot100
 * Description:74. 搜索二维矩阵
 *
 * @Author BCXJ
 * @Create 2025/8/14 08:43
 * @Version 1.0
 * @Since 1.0
 */
public class c064 {
    public static void main(String[] args) {
        int[][] arr = {{1, 1}};
        System.out.println(searchMatrix(arr, 2));
    }

    public static boolean searchMatrix(int[][] matrix, int target) {
        int rowLen = matrix.length;
        int colLen = matrix[0].length;

        int l = 0, r = rowLen * colLen - 1, mid, row, col;
        while (l <= r) {
            mid = l + ((r - l) >> 1);
            row = mid / colLen;
            col = mid % colLen;
            if (matrix[row][col] == target) {
                return true;
            } else if (matrix[row][col] > target) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }

        return false;
    }


    /*
        public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        int left = 0, right = m * n - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            int value = matrix[mid / n][mid % n]; // 一维索引映射到二维

            if (value == target) {
                return true;
            } else if (value < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return false;
    }
     */
}
